<template>
  <div class="container">
    <div class="content">
      <div class="button">
        <div class="btn" @click="close">关闭</div>
        <div class="btn" @click="clear">清空记录</div>
      </div>
      <ul v-if="history.length!==0">
        <li v-for="(item,index) in history" :key="index" @click="close">
          <router-link :to="`/play/${item.href}`">{{item.title}}</router-link>
          <router-link :to="`/play/${item.href}`">继续观看</router-link>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs } from '@vue/runtime-core'
export default {
  emits:['close'],
  setup(props,context){
    const state = reactive({
      history:[]
    })
    onMounted(()=>{
      getHistory()
    })
    const clear = ()=>{
      localStorage.removeItem('movietitle')
      state.history = []
    }
    const close = ()=>{
      context.emit('close')
    }
    const getHistory = async ()=>{
      let data = await localStorage.getItem('movietitle')
      if (data!==null) {
        state.history = JSON.parse(data)
      }
    }
    return{
      ...toRefs(state),
      clear,
      close
    }
  }
}
</script>

<style lang="less" scoped>
.container{
  width: 100%;
  position: absolute;
  top: 50px;
  background: #fff;
  z-index: 100;
  .content{
    margin: 0 10px;
    .button{
      display: flex;
      justify-content: space-between;
      margin-bottom: 30px;
      .btn{
        margin-top: 20px;
        background: #f0f0f0;
        border-radius: 5px;
        text-align: center;
        width: 100px;
        height: 40px;
        line-height: 40px;
        font-size: 16px;
      }
    }
    ul{
      li{
        display: flex;
        justify-content: space-between;
        margin-bottom: 20px;
      }
    }
  }
}
</style>